//package com.yl.common;
//
//import cn.hutool.core.util.RandomUtil;
//import com.aliyuncs.exceptions.ClientException;
//import com.muyu.auth.utils.SmsService;
//import com.muyu.common.core.constant.CacheConstants;
//import com.muyu.common.core.constant.Constants;
//import com.muyu.common.core.constant.SecurityConstants;
//import com.muyu.common.core.constant.UserConstants;
//import com.muyu.common.core.domain.Result;
//import com.muyu.common.core.enums.UserStatus;
//import com.muyu.common.core.exception.ServiceException;
//import com.muyu.common.core.text.Convert;
//import com.muyu.common.core.utils.StringUtils;
//import com.muyu.common.core.utils.ip.IpUtils;
//import com.muyu.common.redis.service.RedisService;
//import com.muyu.common.security.utils.SecurityUtils;
//import com.muyu.common.system.domain.LoginUser;
//import com.muyu.common.system.domain.SysUser;
//import com.muyu.common.system.remote.RemoteUserService;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.concurrent.TimeUnit;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
///**
// * 登录校验方法
// *
// * @author yangle
// */
//@Component
//@Log4j2
//public class SysLoginService {
//    @Autowired
//    private RemoteUserService remoteUserService;
//
//    @Autowired
//    private SysPasswordService passwordService;
//
//    @Autowired
//    private SysRecordLogService recordLogService;
//
//    @Autowired
//    private RedisService redisService;
//
//    private static final String FIXED_CODE = "741852";
//
//
//    /**
//     * 登录
//     */
//    public LoginUser login (String username, String password) {
//        // 用户名或密码为空 错误
//        if (StringUtils.isAnyBlank(username, password)) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
//            throw new ServiceException("用户/密码必须填写");
//        }
//        // 密码如果不在指定范围内 错误
//        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
//                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
//            throw new ServiceException("用户密码不在指定范围");
//        }
//        // 用户名不在指定范围内 错误
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
//            throw new ServiceException("用户名不在指定范围");
//        }
//        // IP黑名单校验
//        String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
//        if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾，访问IP已被列入系统黑名单");
//            throw new ServiceException("很遗憾，访问IP已被列入系统黑名单");
//        }
//        // 查询用户信息
//        Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
//
//        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
//            throw new ServiceException("登录用户：" + username + " 不存在");
//        }
//
//        if (Result.FAIL == userResult.getCode()) {
//            throw new ServiceException(userResult.getMsg());
//        }
//
//        LoginUser userInfo = userResult.getData();
//        SysUser user = userResult.getData().getSysUser();
//        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起，您的账号已被删除");
//            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
//        }
//        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
//            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用，请联系管理员");
//            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
//        }
//        passwordService.validate(user, password);
//        recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
//        return userInfo;
//    }
//
//    public void logout (String loginName) {
//        recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
//    }
//
//    /**
//     * 注册
//     */
//    public void register (String username, String password) {
//        // 用户名或密码为空 错误
//        if (StringUtils.isAnyBlank(username, password)) {
//            throw new ServiceException("用户/密码必须填写");
//        }
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
//            throw new ServiceException("账户长度必须在2到20个字符之间");
//        }
//        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
//                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
//            throw new ServiceException("密码长度必须在5到20个字符之间");
//        }
//
//        // 注册用户信息
//        SysUser sysUser = new SysUser();
//        sysUser.setUserName(username);
//        sysUser.setNickName(username);
//        sysUser.setPassword(SecurityUtils.encryptPassword(password));
//        Result<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
//
//        if (Result.FAIL == registerResult.getCode()) {
//            throw new ServiceException(registerResult.getMsg());
//        }
//        recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
//    }
//
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//
//
//    @Autowired
//    private SmsService smsService;
//    public Result sendCode(String phonenumber) throws ClientException {
//        //用户名或密码错误
//        if(StringUtils.isAnyBlank(phonenumber)){
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "手机号不能为空");
//            return Result.error("手机号不能为空");
//        }
//        //手机号正则验证
//        String regex = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(phonenumber);
//        if (!matcher.matches()){
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "手机号格式不正确");
//            return Result.error("手机号格式不正确");
//        }
//
//        //重复发送校验
//
//        Boolean b = redisTemplate.hasKey(phonenumber);
//        if(b){
//            return Result.error("五分钟内，请勿重复发送");
//        }
//        String code = RandomUtil.randomNumbers(6);
//        if (redisTemplate.hasKey(phonenumber)){
//            redisTemplate.delete(phonenumber);
//        }
////        Random random = new Random();
////        String code = null;
////        code = String.valueOf(random.nextInt(999999));
//        smsService.sendSms(phonenumber,code);
////        smsService.sendLoginVerificationCode(phonenumber,code);
//        log.info("验证码为："+code);
//        redisTemplate.opsForValue().set(phonenumber,code,5, TimeUnit.MINUTES);
//        return Result.success(code,"验证码发送成功");
//    }
//
//    public Result loginByPhone(String phonenumber, String code) {
////        //判断手机号是否为空
////        if(StringUtils.isAnyBlank(phonenumber)){
////            recordLogService.recordLogininfor(phonenumber,Constants.LOGIN_FAIL,"手机号不能为空");
////            return  Result.error("手机号不能为空");
////        }
////        if (code==null || code.equals("")){
////            recordLogService.recordLogininfor(phonenumber,Constants.LOGIN_FAIL,"验证码不能为空");
////            return  Result.error("验证码不能为空");
////        }
////        //判断验证码的长度
////        if(code.length() != UserConstants.CODE_LENGTH){
////            recordLogService.recordLogininfor(phonenumber,Constants.LOGIN_FAIL,"验证码为6位");
////            return  Result.error("验证码为6位");
////        }
////        //判断手机号格式
////        //手机号正则验证
////        String regex = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
////        Pattern pattern = Pattern.compile(regex);
////        Matcher matcher = pattern.matcher(phonenumber);
////        if (!matcher.matches()){
////            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "手机号格式不正确");
////            return Result.error("手机号格式不正确");
////        }
////        //判断验证码是否存在
////        if(!redisTemplate.hasKey(phonenumber)){
////            recordLogService.recordLogininfor(phonenumber,Constants.LOGIN_FAIL,"验证码已过期");
////            return  Result.error("验证码已过期");
////        }
////        //判断验证码是否正确
////        String redisCode = redisTemplate.opsForValue().get(phonenumber);
////        if (!org.apache.commons.lang3.StringUtils.equals(redisCode,code)){
////            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "验证码错误");
////            return  Result.error("验证码错误");
////        }
////        Result<LoginUser> userResult;
////        userResult = remoteUserService.getUserByPhone(phonenumber, SecurityConstants.INNER);
////        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
////            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "登录用户手机号不存在");
////            return  Result.error("登录用户手机号：" + phonenumber + " 不存在");
////
////        }
////        if (Result.FAIL == userResult.getCode())
////        {
////            throw new ServiceException(userResult.getMsg());
////        }
////
////        LoginUser userInfo = userResult.getData();
////        SysUser user = userResult.getData().getSysUser();
////        if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
////        {
////            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "对不起，您的账号已被删除");
////            throw new ServiceException("对不起，您的账号：" + phonenumber + " 已被删除");
////        }
////        if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
////        {
////            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "用户已停用，请联系管理员");
////            throw new ServiceException("对不起，您的账号：" + phonenumber + " 已停用");
////        }
////        recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_SUCCESS, "登录成功");
////        return Result.success(userInfo);
//        // 判断手机号是否为空
//        if (StringUtils.isAnyBlank(phonenumber)) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "手机号不能为空");
//            return Result.error("手机号不能为空");
//        }
//        if (code == null || code.equals("")) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "验证码不能为空");
//            return Result.error("验证码不能为空");
//        }
//        // 判断验证码的长度
//        if (code.length() != UserConstants.CODE_LENGTH) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "验证码为6位");
//            return Result.error("验证码为6位");
//        }
//        // 判断手机号格式
//        // 手机号正则验证
//        String regex = "^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$";
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(phonenumber);
//        if (!matcher.matches()) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "手机号格式不正确");
//            return Result.error("手机号格式不正确");
//        }
//        //查询手机号是否存在
//        Result<Boolean> booleanR = remoteUserService.queryPhonenumber(phonenumber, SecurityConstants.INNER);
//        log.info("查询结果："+booleanR);
//
//        // 先检查 booleanR 和其数据是否为 null
//        if (booleanR == null || booleanR.getData() == null) {
//            SysUser sysUser = new SysUser();
//            //获取登录ip
//            sysUser.setLoginDate(new Date());
//            sysUser.setPhonenumber(phonenumber);
//            sysUser.setUserName(phonenumber);
//            sysUser.setPassword(phonenumber);
//            sysUser.setNickName(phonenumber);
//            remoteUserService.add(sysUser);
//        }
//
//        // 判断验证码是否正确
//
//        if (!redisTemplate.hasKey(phonenumber)){
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "验证码已过期");
//            return  Result.error("验证码已过期");
//        }
//        String s = redisTemplate.opsForValue().get(phonenumber);
//        System.out.println(s);
//        if (!redisTemplate.opsForValue().get(phonenumber).equals(code)){
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "验证码错误");
//            return  Result.error("验证码错误");
//        }
//        Result<LoginUser> userResult;
//        userResult = remoteUserService.getUserByPhone(phonenumber, SecurityConstants.INNER);
//        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "登录用户手机号不存在");
//            return Result.error("登录用户手机号：" + phonenumber + " 不存在");
//        }
//        if (Result.FAIL == userResult.getCode()) {
//            throw new ServiceException(userResult.getMsg());
//        }
//
//        LoginUser userInfo = userResult.getData();
//        SysUser user = userResult.getData().getSysUser();
//        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "对不起，您的账号已被删除");
//            throw new ServiceException("对不起，您的账号：" + phonenumber + " 已被删除");
//        }
//        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, "用户已停用，请联系管理员");
//            throw new ServiceException("对不起，您的账号：" + phonenumber + " 已停用");
//        }
//        if (FIXED_CODE.equals(code)) {
//            recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_SUCCESS, "登录成功");
//            return Result.success(userInfo);
//        }
//
//        recordLogService.recordLogininfor(phonenumber, Constants.LOGIN_SUCCESS, "登录成功");
//        return Result.success(userInfo);
//    }
//
//}
